/**
 * File:    ThinningAlgorithm.h
 * Author:  Bui Quang Anh
 * Email:   quang_anh.bui@univ-lr.fr
 * Version: 1.0
 * 
 * DESCRIPTION:
 * Header file for Tools namespace.
 * Provide common functions at are used in whole program
 * 
 * CHANGE HISTORY:
 * (14/10/2011 12:48) File first time created - BUI Quang Anh
 */
#pragma once
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <list>
#include <set>
#include <utility>
#include <sstream>

#include <opencv.hpp>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "Structures.h"

using namespace std;

namespace Tools
{
	// Calculate distance Euclidean between 2 points p1 and p2
	double distance(const CvPoint &p1, const CvPoint &p2);

	// Calculate distance Euclidean between point p0 and line v0v1
	double distance(const CvPoint &p0, const CvPoint &v0, const CvPoint &v1);

	// Calculate estimated stroke width
	// @source: source image
	int calculate_estimated_stroke_width(const IplImage *source);

	// Find contour of an image
	// @source: source image
	// @contour: output, vector that contains contour of source image
	void find_contour(const IplImage* source, vector<CvPoint> &contour);

	// Check if the specified point is a contour point
	// @source: source image
	// @x: X cordinate of the point
	// @y: Y cordinate of the point
	bool check_contour_point(const IplImage* source, int x, int y);


	// Remove spurious loops in image
	// @source: input image
	// @dest: destination image
	void remove_spurious_loops(const IplImage *source, IplImage *dest);

	// Find approximate shortest distance from a specified point to contour
	// @source: original image
	// @point: specified point
	// Return: approximate shortest distance
	double find_approximate_shortest_distance_to_contour(const IplImage* source, CvPoint point);

	// Find connected components in a set of points
	// @S: the set that we want to its connected components
	// Return: list of connected components of S
	void find_connected_components(set<FeaturePoint> S, vector<set<FeaturePoint>> &result);

	// Get all elements in a substroke
	set<CvPoint,ImagePointComp> get_substroke_elements(CvPoint first_point, IplImage* map, CvScalar color, CvScalar &combine_color = cvScalar(0,0,0));
}